package com.onec.service.manage.controller;

import com.onec.service.api.dto.response.BaseResponse;
import com.onec.service.api.enums.ResultCodeEnum;
import com.onec.service.manage.content.TokenContent;
import com.onec.service.manage.entity.UserInfo;
import com.onec.service.manage.service.UserLoginService;
import com.onec.service.manage.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.servlet.http.HttpServletRequest;

/**
 * @author ONEC
 */
public class BaseController {

    @Autowired
    protected StringRedisTemplate stringRedisTemplate;

    @Autowired
    UserLoginService userLoginService;

    /**
     * 返回api成功
     *
     * @return
     */
    protected BaseResponse responseData() {
        return BaseResponse.error(ResultCodeEnum.CLIENT_LOGIN_EXPIRE);
    }

    /**
     * 获取当前登录的用户信息
     *
     * @param httpRequest
     * @return
     */
    protected UserInfo getUserInfoByToken(HttpServletRequest httpRequest) {
        String token = httpRequest.getHeader(TokenContent.TOKEN);
        Long userId = JwtUtil.getUserId(token);
        return userLoginService.findUserById(userId);
    }

    /**
     * 获取当前登录的用户信息
     *
     * @param httpRequest
     * @return
     */
    protected Long getUserIdByToken(HttpServletRequest httpRequest) {
        String token = httpRequest.getHeader(TokenContent.TOKEN);
        return JwtUtil.getUserId(token);
    }
}
